package com.fourGirlsAndADude.mobileDRT;

import java.util.ArrayList;

import com.att.intern.webservice.Incident;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class BuildingInfoAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private static ArrayList<Incident> incidents;
	private final String tag = "CustomAdapter";
	private static ArrayList<BuildingInfo> infos;
	private Context context;
	private Boolean isIncident;

	public BuildingInfoAdapter(Context context, ArrayList<Incident> incidents) {
		Log.v(tag, "In constructor");
		mInflater = LayoutInflater.from(context);
		this.incidents = incidents;
		this.context = context;
		isIncident = true;
	}

	public BuildingInfoAdapter(ArrayList<BuildingInfo> infos, Context context) {
		mInflater = LayoutInflater.from(context);
		this.context = context;
		this.infos = infos;
		isIncident = false;
	}

	@Override
	public boolean areAllItemsEnabled() {
		// TODO Auto-generated method stub
		// return super.areAllItemsEnabled();
		return true;
	}

	@Override
	public boolean isEnabled(int position) {
		// TODO Auto-generated method stub
		// return super.isEnabled(position);
		return true;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (isIncident) {
			return incidents.size();
		} else {
			return infos.size();
		}
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Log.v(tag, "In get View");
		ViewHolder holder;
		Log.v(tag, "1");
		if (convertView == null) {
			Log.v(tag, "2A");
			convertView = mInflater.inflate(R.layout.row, null);
			Log.v(tag, "3");
			holder = new ViewHolder();
			Log.v(tag, "4");
			holder.lock = (ImageView) convertView.findViewById(R.id.lock_icon);
			Log.v(tag, "5");
			holder.name = (TextView) convertView
					.findViewById(R.id.building_name);
			Log.v(tag, "6");
			holder.address = (TextView) convertView
					.findViewById(R.id.building_address);
			Log.v(tag, "7");
			holder.date = (TextView) convertView
					.findViewById(R.id.date_and_star);
			Log.v(tag, "8");
			convertView.setTag(holder);
			Log.v(tag, "9");
		} else {
			holder = (ViewHolder) convertView.getTag();
			Log.v(tag, "2B");
		}

		String sName;
		String sAddress;
		String sDate;
		if (isIncident) {
			if (incidents.get(position).getIncidentStatus().equals("open")) {
				holder.lock.setImageResource(R.drawable.ic_action_lock_open);
			} else {
				holder.lock.setImageResource(R.drawable.ic_action_lock_closed);
			}
			sName = incidents.get(position).getBuildingName();
			if (sName.length() > 15) {
				sName = incidents.get(position).getBuildingName()
						.substring(0, 16)
						+ "...";
			}
			sAddress = incidents.get(position).getBuildingAddress();
			if (sAddress.length() > 25) {
				sAddress = incidents.get(position).getBuildingAddress()
						.substring(0, 26)
						+ "...";
			}
			String date = incidents.get(position).getInitialRptDate();
			sDate = date.substring(6, 7) + "/" + date.substring(8) + "/"
					+ date.substring(2, 4);

		} else {
			holder.lock.setImageResource(R.drawable.ic_action_lock_closed);
			sName = infos.get(position).getBuildingName();
			sAddress = infos.get(position).getAddress();
			sDate = "NA/NA/NA";
		}

		holder.name.setText(sName);
		holder.address.setText(sAddress);
		holder.date.setText(sDate);
//		final View finalView = convertView;
//		convertView.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View arg0) {
//				// TODO Auto-generated method stub
//				if (isIncident) {
//					displayBuildingInfo(incidents.get(position), finalView);
//				} else {
//					displayBuildingInfo(infos.get(position), finalView);
//				}
//			}
//		});
		return convertView;
	}

	static class ViewHolder {
		TextView name;
		TextView address;
		TextView date;
		ImageView lock;
	}

	
}
